package cn.dansj.common.utils.json;

import cn.dansj.common.utils.transfer.DateTime;
import cn.dansj.common.utils.transfer.StringUtils;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

import java.io.IOException;

class LongDeserializer extends JsonDeserializer<Long> {
    public static final LongDeserializer INSTANCE = new LongDeserializer();

    @Override
    public Long deserialize(JsonParser p, DeserializationContext context) throws IOException {
        String text = p.getText();
        try {
            return p.currentToken() == JsonToken.VALUE_STRING ? DateTime.isDateTime(text) ? DateTime.getInstance(text).getTimeInMillis() : Long.parseLong(text.endsWith("L") ? StringUtils.substr(text, 0, -1) : text) : p.getLongValue();
        } catch (Throwable e) {
            throw new IOException("Cannot deserialize to long: " + p.getCurrentToken() + ": " + text);
        }
    }
}